<?php
namespace app\wechat\controller;

class Member extends Common
{
    public function index()
    {
        $param = [
            'member_id' => session('member_id'),
            'terrace' => 'weixin'
        ];
        $data = call_interface('get_member_index', $param);
        if ($data['code'] == 200) {
            $member_info = $data['data'];
            // 如果头像和昵称不存在则使用微信头像和昵称
            if (!isset($member_info['nickname'])) {
                $member_info['nickname'] = session('?nickname') ? session('nickname') : '';
            }
            if (!isset($member_info['headimgurl'])) {
                $member_info['headimgurl'] = session('?headimgurl') ? session('headimgurl') : '';
            }
        } else {
            $member_info = [];
        }
        $this->assign('member', $member_info);

        return view();
    }
    
    // 修改单张图片  （logo  头像）
    public function save_image() {
        $model = input('param.model');
        $field = input('param.field');
        $type = input('param.type');
        if ($model == 'member_role_info') {
            //$where['type'] = $type;
        }
        $where['member_id'] = session('member_id');
        if (request()->isAjax()) {
            $image = input('param.image');
            $rs = db($model)->where($where)->update([$field => $image]);
            if ($rs !== false) {
                return json(['code' => 200, 'msg' => 'SUCCESS']);
            } else {
                return json(['code' => 0, 'msg' => 'FAULT']);
            }
        } else {
            $image = db($model)->where($where)->value($field);
            if ($model == 'member_role_info' && !$image) {
                $image = session('headimgurl');
            }
            $this->assign('image', $image);
            $this->assign('model', $model);
            $this->assign('field', $field);
            $this->assign('type', $type);
        }
        return view();
    }
}
